package com.study.main;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.study.blockingqueue.Consumer;
import com.study.blockingqueue.Fetcher;
import com.study.blockingqueue.Indexer;
import com.study.blockingqueue.Producer;

public class Main {

	private static BlockingQueue<String> queue1 = new ArrayBlockingQueue<String>(1);
	private static BlockingQueue<Integer> queue2 = new ArrayBlockingQueue<Integer>(1);

	public static void main(String[] args) {
		ExecutorService service = Executors.newCachedThreadPool();

//		oneMethod(service);
		secondMethod(service);
	}

	private static void secondMethod(ExecutorService service) {
		Producer p = new Producer(queue2);
		
		Consumer c1 = new Consumer(queue2);
		Consumer c2 = new Consumer(queue2);
		Consumer c3 = new Consumer(queue2);
		Consumer c4 = new Consumer(queue2);

		service.submit(p);
		service.submit(c1);
		service.submit(c2);
		service.submit(c3);
		service.submit(c4);
	}

	private static void oneMethod(ExecutorService service) {
		Fetcher producer = new Fetcher(queue1);
		
		Indexer consumer = new Indexer(queue1);
		
		Indexer consumerSecond = new Indexer(queue1);
		
		service.submit(producer);
		service.submit(consumer);
		service.submit(consumerSecond);

		try {
			Thread.sleep(5000);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

}
